package org.example.boot.usercenter.exception;

import lombok.extern.slf4j.Slf4j;
import org.example.boot.usercenter.common.BaseResponse;
import org.example.boot.usercenter.common.ErrorCode;
import org.example.boot.usercenter.common.ResultUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @InterfaceName: GlobalExceptionHandler
 * @Description: 全局异常处理器
 * @Author: YourName
 * @Date: 2025/1/12
 */
@RestControllerAdvice
@Slf4j //日记输出的注解
public class GlobalExceptionHandler {

    /**
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException e) {
        // 打印日记输出
        log.error("businessException: " + e.getMessage(), e.getDescription(),e);
        return ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription());
    }
    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeException", e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage(), "");
    }
}
